<?php
class Delivery_Model_Mapper_OpcionesFamilias
	extends Delivery_Model_Mapper_Abstract
{
	
	public function getDbTable()
	{
		return parent::getDbTable( 'OpcionesFamilias' );
	}
	
	public function save( $familia )
	{
		$dbTable= $this->getDbTAble();
		
		if ( ! $familia->getId() ){	
			return $familia->idOpcionFamilia = $dbTable->insert( $familia );
		} else {					
			return $dbTable->update( $familia );	
		}	
		
	}
	
	public function delete( $familia ){
	
		$mapper = Delivery_Model_Factory::mapper('Opciones');
	 	$opciones = $mapper->getByOpcionFamilia( $familia->idOpcionFamilia );
		
		if ( count($opciones) ){
			throw new Delivery_Model_Exception( Delivery_Model_Exception::OPCION_FAMILY_CANNOT_BE_DELETED );
		}
				
		$dbTable= $this->getDbTAble();
		
		$this->logDeleted( $familia );
		
		return $dbTable->delete( $familia );
	}
	
	/**
	 * @param int $idCategoria
	 * @return object Delivery_Model_Categoria
	 */
	public function getById( $idFamilia ){
	
		if ( $this->getIdentityMap()->isRegistered( 'Delivery_Model_OpcionFamilia', $idFamilia )){
			return $this->getIdentityMap()->get( 'Delivery_Model_OpcionFamilia', $idFamilia );
		}
		
		$dbTable= $this->getDbTAble();
		$row	= $dbTable->getById( $idFamilia );
	
		if( empty($row) ){
			throw new Delivery_Model_Exception( Delivery_Model_Exception::OPCION_FAMILY_NOT_FOUND );
		}
		
		return $this->_fetch( $row );
	}
	
	/**
	 * @param array ids
	 *  @return Delivery_Model_Collection_Categorias
	 */
	public function getByIds( $ids )
	{
		$dbTable= $this->getDbTAble();
		
		return $this->_fetchCollection(
			'OpcionesFamilias',
			$dbTable->getByIds( $ids )
		);
	}
	
	/**
	 * @return Delivery_Model_Collection_Categorias
	 */
	public function getAll(){
	
		$dbTable= $this->getDbTAble();
			
		return $this->_fetchCollection(
			'OpcionesFamilias',
			$dbTable->getAll()
		);
	}	
	
	/**
	 * @param array
	 * @return array objetos familia
	 */
	public function getByParams( $params, $offset = null, $limit = null, $order = null ){
		
		$dbTable = $this->getDbTAble();
			
		return $this->_fetchCollection(
			'OpcionesFamilias',
			$dbTable->getByParams( $params, $offset, $limit, $order )
		);
	}
	
	/**
	 * @param array
	 * @return int
	 */
	public function countByParams( $params ){
		return $this->getDbTAble()->countByParams($params);
	}
	
	
	/**
	 * @param array
	 * @return object Delivery_Model_Categoria
	 */
	protected function _fetch( $row )
	{
		$familia = new Delivery_Model_OpcionFamilia( $row );
		
		$this->getIdentityMap()->set( $familia, $familia->getId() );
		
		return $familia;
	}
	
}